<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="项目名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入项目名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
        <el-input
            v-model="queryParams.postName"
            placeholder="请输入岗位名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="岗位状态" clearable style="width: 200px">
          <el-option
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务分类" prop="###">
<!--        {{queryParams.category}}-->
        <el-cascader
            v-model="queryParams.category"
            :options="options"
            :props="props"
            @change="handleChange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:post:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="postId" />
      <el-table-column label="项目名称" align="center" prop="name" />
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="业务领域" align="center" prop="category"></el-table-column>
      <el-table-column label="项目成员" align="center" prop="person" />
      <el-table-column label="开始时间" align="center" prop="startDate" width="180">
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDate" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:post:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="600px"  append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目年份" prop="year">
          <el-date-picker
              v-model="form.year"
              type="year"
              placeholder="请输入年份"
              format="YYYY"
              value-format="YYYY"
          />

        </el-form-item>
        <el-form-item label="项目起止时间" prop="rangeDate">
          <el-date-picker
              v-model="form.rangeDate"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              :size="size"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item label="项目人员" prop="person">
          <el-select
              v-model="form.person"
              placeholder="请输入项目人员"
              multiple
          >
            <el-option key="张三" label="张三" value="张三"></el-option>
            <el-option key="李四" label="李四" value="李四"></el-option>
            <el-option key="王五" label="王五" value="王五"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="业务领域" prop="category">
          <el-cascader
              v-model="form.category"
              :options="options"
              :props="props"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import {delPost, updatePost, addBack, listBack, getBack, updateBack, delBack} from "@/api/system/post";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,

    name: undefined,
    status: undefined
  },
  rules: {
    name: [{ required: true, message: "项目名称不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

// watchEffect(()=>{
//   if (form.value.rangeDate) {
//     f
//   }
// })
/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listBack(queryParams.value).then(response => {
    postList.value = response.data.list;
    total.value = response.data.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
  };
  proxy.resetForm("postRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize = 10;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  form.value = {}
  queryParams.value = {}
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加岗位";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getBack(id).then(response => {
    form.value = response.data;
    if (form.value.startDate && form.value.endDate){
      form.value.rangeDate = [form.value.startDate, form.value.endDate]
    }
    open.value = true;
    title.value = "修改项目";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate(valid => {
    if (valid) {
      if (form.value.rangeDate){
        form.value.startDate = form.value.rangeDate[0];
        form.value.endDate = form.value.rangeDate[1];
      }
      if (form.value.id != undefined) {
        updateBack(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBack(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(()=>
     delBack(postIds)
  ).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/post/export", {
    ...queryParams.value
  }, `post_${new Date().getTime()}.xlsx`);
}

getList();

import { ref } from 'vue'

const value = ref([])

const props = {
  expandTrigger: 'hover',
}

const handleChange = (value) => {
  console.log(value)
}

const options = [
  {
    value: '战略决策',
    label: '战略决策',
    children: [
      {
        value: '战略管理',
        label: '战略管理',
      },
      {
        value: '决策支持',
        label: '决策支持',
      },
    ],
  },
  {
    value: '运输生产',
    label: '运输生产',
    children: [
      {
        value: '移动装备管理',
        label: '移动装备管理',
      },
    ],
  },
  {
    value: '综合协同',
    label: '综合协同',
    children: [
      {
        value: '办公管理',
        label: '办公管理',
      },
    ],
  },
]
</script>
